/**
 * 应用程序系统相关Store
 */
 import utils from "@/common/utils";
 import defaultSettings from "@/settings";
 import { login, refreshToken, getUserInfo } from "@/api/system";
 import { getToken, setToken, removeToken } from "@/common/auth";
 
 const getDefaultState = () => {
   return {
     token: getToken(),
     refreshtoken: getToken(),
     tokentype: "Bearer",
     authtime: 0,
     expiresat: 0,
     islogin: false,
     accid: "",
     userid: "",
     username: "",
     nickname: "",
     roles: [],
     usecases: [],
     avatar: "",
   };
 };
 
 const state = getDefaultState();
 
 const mutations = {
   RESET_STATE: (state) => {
     removeToken();
     Object.assign(state, getDefaultState());
   },
   CHANGE_STATE: (state, { key, value }) => {
     if (Object.prototype.hasOwnProperty.call(state, key)) {
       state[key] = value;
     }
   },
   SET_TOKEN: (
     state,
     { token, refreshtoken, tokentype, authtime, expiresat }
   ) => {
     state.token = token;
     state.refreshtoken = refreshtoken;
     state.tokentype = tokentype;
     state.authtime = authtime;
     state.expiresat = expiresat;
     state.islogin = true;
     const expires = new Date(expiresat * 1000);
     setToken(token, expires);
   },
   SET_ACCOUNT: (
     state,
     { accid, userid, username, nickname, roles, usecases }
   ) => {
     state.accid = accid;
     state.userid = userid;
     state.username = username;
     state.nickname = nickname;
     state.roles = roles;
     state.usecases = usecases;
     // state.avatar = avatar;
   },
 };
 
 const actions = {
   // reset state
   resetState({ commit }) {
     commit("RESET_STATE");
   },
   // change state
   changeState({ commit }, data) {
     commit("CHANGE_STATE", data);
   },
 
   // user login
   login({ commit }, userInfo) {
     const { username, password } = userInfo;
     return new Promise((resolve, reject) => {
       if (utils.isEmpty(username)) throw new Error("请输入账号");
       if (utils.isEmpty(password)) throw new Error("请输入密码");
       login({
         username: username.trim(),
         password: password.trim(),
         grant_type: defaultSettings.grant_type,
       })
         .then((response) => {
           // console.log("登录请求返回数据", response);
           try {
             // const { data } = response;
             const data = response;
             if (utils.isEmpty(data)) throw new Error("验证出错，请重新登录");
             const { fuserid, fusername, ffullname, roles, usecases } = data;
             if (utils.isEmpty(roles) || roles.length <= 0)
               throw new Error("无可用角色，请联系管理员");
             if (utils.isEmpty(usecases) || usecases.length <= 0)
               throw new Error("权限不足，请联系管理员");
			   sessionStorage.setItem('token',data.ftoken)
             commit("SET_TOKEN", {
               token: data.ftoken,
               refreshtoken: data.ftoken,
               tokentype: "Bearer",
               authtime: new Date().getTime(),
               expiresat: 24 * 60 * 60,
             });
             commit("SET_ACCOUNT", {
               accid: fuserid,
               userid: fuserid,
               username: fusername,
               nickname: ffullname,
               roles: roles,
               usecases: usecases,
               // avatar: avatar,
               // avatar: headGif,
             });
             resolve(data);
           } catch (error) {
             reject(error);
           }
         })
         .catch((error) => {
           reject(error);
         });
     });
   },
 
   // user logout
   logout({ commit }) {
     return new Promise((resolve) => {
       commit("RESET_STATE");
       this.dispatch("permission/resetState");
       resolve();
       // logout(state.token).then(() => {
       //     commit('RESET_STATE');
       //     this.dispatch("permission/resetState");
       //     resolve();
       // }).catch(error => { reject(error); });
     });
   },
 
   // refresh token
   refreshToken({ commit, state }) {
     return new Promise((resolve, reject) => {
       if (utils.isEmpty(state.refreshtoken)) resolve();
       else {
         refreshToken({ refreshtoken: state.refreshtoken })
           .then((response) => {
             try {
               // const { data } = response;
               const data = response;
               if (utils.isEmpty(data)) throw new Error("验证出错，请重新登录");
               const { fuserid, fusername, ffullname, roles, usecases } = data;
               if (utils.isEmpty(roles) || roles.length <= 0)
                 throw new Error("无可用角色，请联系管理员");
               if (utils.isEmpty(usecases) || usecases.length <= 0)
                 throw new Error("权限不足，请联系管理员");
               commit("SET_TOKEN", {
                 token: data.ftoken,
                 refreshtoken: data.ftoken,
                 tokentype: "Bearer",
                 authtime: new Date().getTime(),
                 expiresat: 24 * 60 * 60,
               });
               commit("SET_ACCOUNT", {
                 accid: fuserid,
                 userid: fuserid,
                 username: fusername,
                 nickname: ffullname,
                 roles: roles,
                 usecases: usecases,
                 // avatar: avatar,
                 // avatar: headGif,
               });
               resolve(data);
             } catch (error) {
               reject(error);
             }
           })
           .catch(() => {
             // 错误信息提示出来，不再将错误往上抛
             resolve(); // reject(error);
           });
       }
     });
   },
 
   // remove token
   resetToken({ commit }) {
     return new Promise((resolve) => {
       commit("RESET_STATE");
       this.dispatch("permission/resetState");
       resolve();
     });
   },
 
   // get user info
   getUserInfo({ commit }) {
     return new Promise((resolve, reject) => {
       getUserInfo()
         .then((response) => {
           try {
             // const { data } = response;
             const data = response;
             if (utils.isEmpty(data)) throw new Error("验证出错，请重新登录");
             const { accountid, userid, username, nickname, roles, usecases } =
               data;
             if (utils.isEmpty(roles) || roles.length <= 0)
               throw new Error("无可用角色，请联系管理员");
             if (utils.isEmpty(usecases) || usecases.length <= 0)
               throw new Error("权限不足，请联系管理员");
             commit("SET_ACCOUNT", {
               accid: accountid,
               userid: userid,
               username: username,
               nickname: nickname,
               roles: roles,
               usecases: usecases,
               // avatar: avatar,
               // avatar: headGif,
             });
             resolve(data);
           } catch (error) {
             reject(error);
           }
         })
         .catch((error) => {
           reject(error);
         });
     });
   },
 };
 
 export default { namespaced: true, state, mutations, actions };
 